package com.ruoyi.zxgl.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.zxgl.domain.ZxglZxtbFj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.zxgl.domain.ZxglZxtb;
import com.ruoyi.zxgl.service.IZxglZxtbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 执行情况填报Controller
 *
 * @author huacai
 * @date 2024-07-30
 */
@RestController
@RequestMapping("/zxgl/zxtb")
public class ZxglZxtbController extends BaseController
{
    @Resource
    private IZxglZxtbService zxglZxtbService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询执行情况填报列表
     */
    @PreAuthorize("@ss.hasPermi('zxgl:zxtb:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZxglZxtb zxglZxtb)
    {
        startPage();
        List<ZxglZxtb> list = zxglZxtbService.selectZxglZxtbList(zxglZxtb);
        return getDataTable(list);
    }

    /**
     * 导出执行情况填报列表
     */
    @PreAuthorize("@ss.hasPermi('zxgl:zxtb:export')")
    @Log(title = "执行情况填报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZxglZxtb zxglZxtb)
    {
        List<ZxglZxtb> list = zxglZxtbService.selectZxglZxtbList(zxglZxtb);
        ExcelUtil<ZxglZxtb> util = new ExcelUtil<ZxglZxtb>(ZxglZxtb.class);
        util.exportExcel(response, list, "执行情况填报数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ZxglZxtb> util = new ExcelUtil<ZxglZxtb>(ZxglZxtb.class);
        util.importTemplateExcel(response, "执行情况填报数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "执行情况填报", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('zxgl:zxtb:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<ZxglZxtb> util = new ExcelUtil<ZxglZxtb>(ZxglZxtb.class);
        InputStream inputStream = file.getInputStream();
        List<ZxglZxtb> list = util.importExcel(inputStream );
        inputStream.close();
        int count = zxglZxtbService.batchInsertZxglZxtb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取执行情况填报详细信息
     */
    @PreAuthorize("@ss.hasPermi('zxgl:zxtb:query')")
    @GetMapping(value = "/{zxtbId}")
    public AjaxResult getInfo(@PathVariable("zxtbId") String zxtbId)
    {
        return success(zxglZxtbService.selectZxglZxtbByZxtbId(zxtbId));
    }

    /**
     * 新增执行情况填报
     */
    @PreAuthorize("@ss.hasPermi('zxgl:zxtb:add')")
    @Log(title = "执行情况填报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZxglZxtb zxglZxtb) {
        zxglZxtb.setUserId(Math.toIntExact(getUserId()));
        int i = zxglZxtbService.insertZxglZxtb(zxglZxtb);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(zxglZxtb.getZxtbId());
        infoRunTaskData.setBusinessType(6); //业务模块: 执行管理-执行情况填报
        infoRunTaskData.setBusinessTitle(zxglZxtb.getKtName()); //任务名称: 课题名称
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(i);
    }

    /**
     * 修改执行情况填报
     */
    @PreAuthorize("@ss.hasPermi('zxgl:zxtb:edit')")
    @Log(title = "执行情况填报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZxglZxtb zxglZxtb) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(zxglZxtb.getKtName()); //任务名称: 课题名称
        infoRunTaskData.setBusinessKey(zxglZxtb.getZxtbId());
        infoRunTaskData.setBusinessType(6); //业务模块: 执行管理-执行情况填报
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(zxglZxtbService.updateZxglZxtb(zxglZxtb));
    }

    /**
     * 删除执行情况填报
     */
    @PreAuthorize("@ss.hasPermi('zxgl:zxtb:remove')")
    @Log(title = "执行情况填报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{zxtbIds}")
    public AjaxResult remove(@PathVariable String[] zxtbIds) {
        //同时删除业务关系数据
        for (String zxtbId : zxtbIds) {
            ZxglZxtb zxglZxtb = zxglZxtbService.selectZxglZxtbByZxtbId(zxtbId);
            iInfoRunTaskDataService.deleteData(zxglZxtb.getZxtbId(), 6);//业务模块: 人才平台-人员申请
        }
        return toAjax(zxglZxtbService.deleteZxglZxtbByZxtbIds(zxtbIds));
    }

    /**
     * 获得执行填报主键ID (UUID)
     */
    @GetMapping("/getZxTbId")
    public AjaxResult getZxTbId(){
        String zxTb = IdUtils.fastSimpleUUID();
        return success(zxTb);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertZxTbFjByZxTbId(@RequestBody ZxglZxtbFj zxglZxtbFj) {
        return toAjax(zxglZxtbService.insertZxTbFjByZxTbId(zxglZxtbFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectZxTbFjList(String sId){
        ZxglZxtbFj zxglZxtbFj = new ZxglZxtbFj();
        zxglZxtbFj.setZxtbId(sId);
        return AjaxResult.success(zxglZxtbService.selectZxTbFjList(zxglZxtbFj));
    }

    /**
     * 删除执行填报附件
     */
    @Log(title = "执行填报附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds)
    {
        return toAjax(zxglZxtbService.deleteZxglZxtbFjByFjIds(fjIds));
    }

}
